<?php
/*
 * web2.0  qq协议主程序
 */
include 'class/poll.class.php';
include 'class/common.class.php';
/**
 * Description of QQ
 *
 * @author seven
 */
class Webqq {
    private $node_hash_service = true;//node 服务中 计算获得好友列表中 哈希服务的状态  true为服务已经开启 flash为关闭
    private $speak_service = true;//bot的语言服务是否已经开启
    private $sms_service = true;// 短信服务是否开启
    private $get_friend_information_lock;//获得朋友信息锁  如果为flash 那么就是获得好友列表失败
    private $qq;
    const MESSAGE = 'message';//个人消息
    const GROUP_MESSAGE = 'group_message';//群消息
    const FILE_MESSAGE = 'file_message';//文件消息
    const VERIFY_REQUIRED = 'verify_required';//好友验证
    const BUDDIES_STATUS_CHANGE = 'buddies_status_change';
    public function __construct() {
        $this->qq = new stdClass();
    }
    
    /*
     * 登陆qq
     * return void
     */
    public function login($qq_num,$passwd)
    {        
        \QQ\Common::echo_system("开始登录qq");
        $verify = \QQ\Poll::check_verify($qq_num);
        if (is_array($verify) && $verify[0] == 0)
        {
            \QQ\Common::echo_system("装载验证码成功");
	    $this->qq->verify = $verify[1];
        }
        else
        {
            \QQ\Poll::check_verify_getimage($qq_num);
            $this->qq->verify = \QQ\Common::scanf('需要输入验证码:');
        }
        
        //生成密码
        $passwd = \QQ\Poll::jspassword($verify[2],$passwd,  $this->qq->verify);
        //登录  这里是登录webqq网页  而不是登录webqq中的 qq系统
        $login = \QQ\Poll::login($qq_num , $passwd, $this->qq->verify);
        if (is_array($login) && $login['4'] == "登录成功！")
        {
                \QQ\Common::echo_system("初次登陆成功:".$login['5']);
        }
        elseif (isset($login[4]))
        {
                \QQ\Common::echo_error("初次登陆失败:".$login[4]);
                return false;
        }
        else
        {
            
                \QQ\Common::echo_error("初次登陆失败,未知的错误");
                return false;
        }
        \QQ\Poll::p_skey($login['2']);
         
        // 获取cookie信息
        $this->qq->cookie = json_decode(json_encode(\QQ\Poll::get_cookie()));
        // 生成客户端id
        $this->qq->clientid = mt_rand(50888888,80888888);
        // 真正的上线 这里才是真的登录web qq中的qq
        $this->qq->login = json_decode(\QQ\Poll::login2($this->qq->cookie->ptwebqq, $this->qq->clientid));

        if (!empty($this->qq->login) and $this->qq->login->retcode == 0)
        {
            $this->qq->number = $qq_num;
            $this->qq->uin =& $this->qq->login->result->uin;
            \QQ\Common::echo_system("已成功上线");
        }
        else
        {
            \QQ\Common::echo_error("登陆失败,可能原因：vfwebqq参值不正确");
            return false;
        }
        if(!$this->_group_list())
        {
            return false;
        }
        if(!$this->_friend_list())
        {
            return false;
        }
        return true;
    }
    
    /*
     * 获得群列表
     * return void
     */
    protected function _group_list()
    {
        \QQ\Common::echo_system("开始获得群列表信息");
        //echo $this->qq->login->result->vfwebqq,"\n",$this->qq->number,"\n",$this->qq->cookie->ptwebqq,"\n";
        /***获得群列表**/
        if(!isset($this->qq->login->result->vfwebqq))
        {
            \QQ\Common::echo_system("发生了一个意外,登陆的信息没有返回,试试重新运行吧");
            return false;
        }
        $t = \QQ\Poll::get_group_name_list_mask(
                $this->qq->login->result->vfwebqq,
                $this->qq->number,
                $this->qq->cookie->ptwebqq
        );
        $qq_group = json_decode($t);

        $this->qq->group_list = new stdClass;
        $this->qq->group_code = new stdClass;
        $this->qq->group_flag = new stdClass;
        if($qq_group->retcode == '0')
        {
            foreach($qq_group->result->gnamelist as $item)
            {
                $this->qq->group_list->{$item->gid} = $item->name;
                $this->qq->group_code->{$item->gid} = $item->code;
                $this->qq->group_flag->{$item->gid} = $item->flag;
            }
            \QQ\Common::echo_system("群列表信息获取完成");
        }
        else
        {
            \QQ\Common::echo_error("群列表信息获取失败");
        }
        return true;
    }
    
    /*
     * 获得好友列表
     * return void
     */
    protected function _friend_list()
    {
        \QQ\Common::echo_system("开始获得好友列表状态");
        /********获得好友列表状态   如果获得好友列表失败   那么在下面聊天的时候 也不会获得好友的详细信息******/
        $this->get_friend_information_lock = false;
        if($this->node_hash_service)
        {
            //获得好友列表
            $this->qq->friend_list = json_decode(\QQ\Poll::get_user_friend($this->qq->login->result->vfwebqq,$this->qq->number,$this->qq->cookie->ptwebqq));
            if($this->qq->friend_list->retcode != 0)
            {
                \QQ\Common::echo_error("好友列表获取失败");
                \QQ\Common::echo_error($this->qq->friend_list);
            }
            else
            {
                \QQ\Common::echo_system("好友列表获取成功");
                $friend_data = new stdClass;
                $this->get_friend_information_lock = true;
                foreach($this->qq->friend_list->result->info as $item)
                {
                    $this->qq->friend_list_info->{$item->uin} = $item->nick;
                }
            }
        }
        else
        {
            \QQ\Common::echo_error("取消好友列表的获取,因为服务必须的服务为关闭状态");
        }
        return true;
    }
    /*
     * 添加好友
     */
    public function allow_added_request($account)
    {
        \QQ\Poll::allow_added_request($account,$this->qq->login->result->vfwebqq);
    }
    /*
     * 添加好友 并且加对方为好友
     */
    public function allow_and_add($account,$gid = 0, $mname = '')
    {
        \QQ\Poll::allow_and_add($account,$this->qq->login->result->vfwebqq,$gid, $mname);
    }
    /*
     * 拒绝添加
     */
    public function deny_added_request($account,$msg = '')
    {
        \QQ\Poll::deny_added_request($account,$msg,$this->qq->login->result->vfwebqq);
    }
    /*
     * 读取消息
     */
    public function poll()
    {
        $d = \QQ\Poll::poll($this->qq->login->result->psessionid, $this->qq->clientid);
        $msg = json_decode($d);
        $return_msg = array();
        if ($msg->retcode === 0)
        {
            foreach ($msg->result as $v)
            {
                if ($v->poll_type == self::MESSAGE)
                {
                    if(!isset($friend_data->{$v->value->from_uin}))
                    {
                        $friend_data = $this->_friend_data($v,$this->qq->cookie,$this->qq->friend_list_info);
                    }
                    $group_message_msg = '';
                    $_record = $v->value;
                    //$this->_record($_record,"message");记录消息
                    //处理消息
                    foreach ($v->value->content as $t_id => $t_value)
                    {
                        if($t_id == '0')
                        {
                            continue;
                        }
                        if(!is_array($t_value))
                        {
                            $group_message_msg .= $t_value;
                        }
                        else
                        {
                            if($t_value[0] == 'face') //是qq表情
                            {
                                    $group_message_msg .= "\{face:{$t_value[1]}\}";
                            }
                            elseif($t_value[0] == 'offpic')//是自定义表情
                            {
                                if($t_value[1]->success == '1')
                                {
                                    $group_message_msg .= "\{offpic:{$t_value[1]->file_path}\}";
                                }
                                else
                                {
                                    $group_message_msg .= "\{offpic:error\}";
                                }
                            }
                        }
                    }
                    /*******获得回复内容******/
                    //$t_msg = $this->message_msg($group_message_msg,$v->value->from_uin);
                    /*******回复信息******/
                    //QQ::send_buddy_msg($v->value->from_uin, $t_msg, $this->qq->login->result->psessionid, $this->qq->clientid);
                    $db_msg_data = json_encode(array('msg'=>$group_message_msg,'date'=>time()));

                    $name = isset($friend_data->{$v->value->from_uin}->nick)?$friend_data->{$v->value->from_uin}->nick:false;
                    $return_msg[] = array('type'=>  self::MESSAGE,'value'=>$v,'uin'=>$v->value->from_uin,'msg'=>$group_message_msg,'username'=>$name);
                }

                // 针对群
                else if ($v->poll_type == self::GROUP_MESSAGE)
                {
                    $_record = $v->value;
                    $group_message_msg = '';
                    //$this->_record($_record,"group_message");
                    if(!isset($this->qq->group_user_info->{$v->value->from_uin}))
                    {
                        $this->qq->group_user_info->{$v->value->from_uin} = $this->_group_data($v,$this->qq->group_code,$this->qq->cookie,$this->qq->qqlogin);
                    }

                    // 处理简单表情
                    foreach ($v->value->content as $t_id => $t_value)
                    {
                        if($t_id == '0')
                        {
                            continue;
                        }
                        if(!is_array($t_value))
                        {
                            $group_message_msg .= $t_value;
                        }
                        else
                        {
                            if($t_value[0] == 'face') //是qq表情
                            {
                                    $group_message_msg .= "\{face:{$t_value[1]}\}";
                            }
                            elseif($t_value[0] == 'offpic')//是自定义表情
                            {
                                if($t_value[1]->success == '1')
                                {
                                    $group_message_msg .= "\{offpic:{$t_value[1]->file_path}\}";
                                }
                                else
                                {
                                    $group_message_msg .= "\{offpic:error\}";
                                }
                            }
                        }
                    }

                    $group_name = isset($this->qq->group_list->{$v->value->from_uin})?$this->qq->group_list->{$v->value->from_uin}:'未知群';

                    $msg_info_user = isset($this->qq->group_user_info->{$v->value->from_uin}->cards->{$v->value->send_uin})?
                    $this->qq->group_user_info->{$v->value->from_uin}->cards->{$v->value->send_uin}:
                    $this->qq->group_user_info->{$v->value->from_uin}->minfo->{$v->value->send_uin}->nick;

                    $card = $this->qq->group_user_info->{$v->value->from_uin}->cards->{$this->qq->uin}?
                            $this->qq->group_user_info->{$v->value->from_uin}->cards->{$this->qq->uin}:
                            $this->qq->group_user_info->{$v->value->from_uin}->minfo->{$this->qq->uin}->nick;
                    $return_msg[] = array(
                        'type'=>self::GROUP_MESSAGE,
                        'uin'=>$v->value->from_uin,
                        'msg'=>$group_message_msg,
                        'username'=>$group_name,
                        'send_name'=>$msg_info_user,
                        'value'=>$v,
                        'card'=>$card
                    );
                }


                // 如果是发送文件
                else if ($v->poll_type == "file_message")
                {
                    print_r($v);
                }
                // 如果是 系统消息
                else if ($v->poll_type == "system_message")
                {
                    if($v->type == self::VERIFY_REQUIRED)//好友验证
                    {
                        $return_msg[] = array(
                            'type'=>self::VERIFY_REQUIRED,
                            'uin'=>$v->value->from_uin,
                            'account'=>$v->value->account,
                            'msg'=>$v->value->msg,
                            'value'=>$v,
                            'username'=>$group_name
                        );
                    }
                    else
                    {
                        print_r($v);
                    }
                }
                // 如果是 系统消息 上线 下线通知
                else if ($v->poll_type == self::BUDDIES_STATUS_CHANGE)
                {
                        $return_msg[] = array(
                            'type'=>self::BUDDIES_STATUS_CHANGE,
                            'uin'=>$v->value->from_uin,
                            'msg'=>'',
                            'status'=>$v->value->status, //online上线  offline离线
                            'username'=>$v->value->uin,
                            'value'=>$v,
                            'client_type'=>$v->value->client_type,//客户端id  1是电脑
                        );
                }
                // 如果是 窗口抖动
                else if ($v->poll_type == "shake_message")
                {
                    print_r($v);
                }
                // 如果是 窗口抖动
                else if ($v->poll_type == "shake_message")
                {
                    print_r($v);
                }
                else
                {
                    print_r($v);
                }
            }
            
        }
        if ($msg->retcode == 121)
        {
                return 121;
        }
        return (count($return_msg) >= 1)?$return_msg:false;
    }
    /*
     * 群消息 回复消息
     * @param string from_uin 群的uin
     * @param string msg 回复的消息
     * return void
     */
    public function send_group($from_uin,$msg)
    {
        \QQ\Poll::send_qun_msg($from_uin, $msg, $this->qq->login->result->psessionid, $this->qq->clientid);
    }
    /*
     * 个人消息 回复消息
     * @param string from_uin 用户的uin
     * @param string msg 回复的消息
     * return void
     */
    public function send_buddy($from_uin,$msg)
    {
        \QQ\Poll::send_buddy_msg($from_uin, $msg, $this->qq->login->result->psessionid, $this->qq->clientid);
    }
    /*
     * 返回群成员
     * return objetc
     */
    public function get_group_user()
    {
        return $this->qq->group_user_info;
    }
    /*
     * 返回好友
     * return objetc
     */
    public function get_friend()
    {
        return $this->qq->friend_list_info;
    }
    /*
     * 记录qq消息
     *
     *
     */
    private function _record($value,$type)
    {
	$this->qq_chats_model->add($value->msg_id,$value->from_uin,$value->to_uin,$value->msg_id2,$value->msg_type,$value->reply_ip,$value->time,var_export($value->content,true),$type);
    }

    
    /*
     * 获得群回复的语句
     */
    protected function group_message_msg($content,$qq)
    {
        if(preg_match('|seven::|', $content))
        {
            return Socket_post::speak($content, 'QQ_'.$qq);
        }
        else
        {
            return false;
        }
    }
    
    /*
     * 获得好友详细信息
     *
     */
    private function _friend_data($v,$cookie,$friend_list_info)
    {
            //////////////////////////////******获得发信息qq的详细信息**********////////////////
            $t =  json_decode(\QQ\Poll::get_friend_data_qq($v->value->from_uin,$this->qq->login->result->vfwebqq)); //获得qq信息
            if($this->get_friend_information_lock !== false)
            {
                    $get_friend_information =  json_decode(\QQ\Poll::get_friend_information($v->value->from_uin,$this->qq->login->result->vfwebqq)); //获得个人资料
            }

            /****************获得qq资料*********************/
            $t_qq_number = json_decode(\QQ\Poll::get_friend_qq($v->value->from_uin,$this->qq->login->result->vfwebqq)); 
            $friend_data->{$v->value->from_uin} = new stdClass;
            if($t_qq_number->retcode === 0){
                    $friend_data->{$v->value->from_uin}->qq = $t_qq_number->result->account; //qq号码
            }
            else
            {
                    \QQ\Common::echos("获得qq号失败");
            }
            if($t->retcode === 0){
                    $friend_data->{$v->value->from_uin}->level = $t->result->level; //qq等级
                    $friend_data->{$v->value->from_uin}->days = $t->result->days; //qq使用时间
                    $friend_data->{$v->value->from_uin}->nick =  $friend_list_info->{$v->value->from_uin}; //qq昵称
                    $friend_data->{$v->value->from_uin}->tuin =  $t->result->tuin;
            }
            else
            {
                    \QQ\Common::echos("获得qq资料失败");
            }
            if($get_friend_information->retcode === 0){
                    $friend_data->{$v->value->from_uin}->blood =  $get_friend_information->result->blood;
                    $friend_data->{$v->value->from_uin}->city =  $get_friend_information->result->city;
                    $friend_data->{$v->value->from_uin}->college =  $get_friend_information->result->college;
                    $friend_data->{$v->value->from_uin}->constel =  $get_friend_information->result->constel;
                    $friend_data->{$v->value->from_uin}->country =  $get_friend_information->result->country;
                    $friend_data->{$v->value->from_uin}->email =  $get_friend_information->result->email;
                    $friend_data->{$v->value->from_uin}->face =  $get_friend_information->result->face;
                    $friend_data->{$v->value->from_uin}->gender =  $get_friend_information->result->gender;
                    $friend_data->{$v->value->from_uin}->homepage =  $get_friend_information->result->homepage;
                    $friend_data->{$v->value->from_uin}->mobile =  $get_friend_information->result->mobile;
                    $friend_data->{$v->value->from_uin}->occupation =  $get_friend_information->result->occupation;
                    $friend_data->{$v->value->from_uin}->personal =  $get_friend_information->result->personal;
                    $friend_data->{$v->value->from_uin}->phone =  $get_friend_information->result->phone;
                    $friend_data->{$v->value->from_uin}->province =  $get_friend_information->result->province;
                    $friend_data->{$v->value->from_uin}->shengxiao =  $get_friend_information->result->shengxiao;
                    $friend_data->{$v->value->from_uin}->stat =  $get_friend_information->result->stat;
                    $friend_data->{$v->value->from_uin}->vip_info =  $get_friend_information->result->vip_info;
            }
            else
            {
                    \QQ\Common::echo_error("获得个人资料失败");
                    $this->get_friend_information_lock = false;
            }
            return $friend_data;
    }

	/*
	 * 获得群成员列表
	 *
	 *
	 */

	protected function _group_data($v,$qq_group_code,$cookie,$login)
	{
		$this->qq->group_user_info = json_decode(\QQ\Poll::get_group_info_ext($qq_group_code->{$v->value->from_uin},$this->qq->login->result->vfwebqq));
		$this->qq->group_user_info_detailed = new stdClass;
		$this->qq->group_user_info_detailed->ginfo = $this->qq->group_user_info->result->ginfo;
		if($this->qq->group_user_info->retcode == 0)
		{
		    foreach($this->qq->group_user_info->result->minfo as $item)
		    {
				$item = (object)$item;
		        $this->qq->group_user_info_detailed->minfo->{$item->uin} = (object)$item;
		    }
		    foreach($this->qq->group_user_info->result->vipinfo as $item)
		    {
		        $this->qq->group_user_info_detailed->vipinfo = $item;
		    }
		    if(isset($this->qq->group_user_info->result->cards)) //群备注
		    {
		        foreach($this->qq->group_user_info->result->cards as $item)
		        {
		            $this->qq->group_user_info_detailed->cards->{$item->muin} = $item->card;
		            if($item->muin == $login->result->uin)
		            {
		                $this->qq->group_user_info_detailed->card = $item->card;//自己在群里的备注名字
		            }
		        }
		    }
		}
		else
		{
			return false;
		}
		return $this->qq->group_user_info_detailed;
	}
   
}
